package com.etxxy.common.util;

import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @category MD5工具类
 * @author yiz
 * @date 2016年5月13日 上午8:46:15
 * @version 1.0.0 
 * @copyright pycredit.cn
 */
public final class MD5Util {
	private static final Logger logger = LoggerFactory.getLogger(MD5Util.class);

	/**
	 * 对字符串进行MD5加密
	 * 
	 * @param plainText String
	 * @return String
	 */
	public static String md5(String plainText) {
		MessageDigest md = null;
		byte[] byteArray = null;
		int i = 0;
		StringBuffer buf = null;

		try {
			md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byteArray = md.digest();
			buf = new StringBuffer("");
			for (byte element : byteArray) {
				i = element;
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			return buf.toString();
		} catch (Exception e) {
			logger.error(e.toString(), e);
			return "";
		}
	}
}
